package com.zy.test.threaddemo;


import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * @author MissZhou
 */
public class CreateByTheardPool {

    public static void main(String[] args) {
        //单个线程的线程池（不推荐使用Executors方式新建线程池）
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("ExecutorService 创建线程！");
            }
        });
        //销毁线程池
        executorService.shutdown();

        //使用自定义线程池（推荐）
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-poo1-%d").build();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 200, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        pool.execute(() -> System.out.println("labmda表达式-打印线程名称："+ Thread.currentThread().getName()));
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程名称"+Thread.currentThread().getName());
            }
        });
        pool.shutdown();
    }
}
